package com.woniuxy.oldobject.service.impl;

import com.woniuxy.oldobject.dao.RoomDao;
import com.woniuxy.oldobject.dto.RoomDto;
import com.woniuxy.oldobject.entity.Room;
import com.woniuxy.oldobject.entity.Roomtype;
import com.woniuxy.oldobject.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author： Brian
 * @Todo：
 * @Date： 2022/7/22 17:32
 * @Version： 1.0
 */
@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomDao roomDao;
    @Autowired
    RedisTemplate redisTemplate;

    private static final String ROOMTYPEKEY = "ROOMTYPE";

    /**
     * 查询房间管理
     *
     * @param roomId 房间管理主键
     * @return 房间管理
     */
    @Override
    public Room selectRoomByRoomId(Long roomId) {
        return roomDao.selectRoomByRoomId(roomId);
    }

    /**
     * 查询房间管理列表
     *
     * @param room 房间管理
     * @return 房间管理
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomDao.selectRoomList(room);
    }

    /**
     * 新增房间管理
     *
     * @param room 房间管理
     * @return 结果
     */
    @Override
    public int insertRoom(RoomDto room) {
        room.setCreateTime(new Date());
        room.getNewRoomType().setRoomtypeNum(room.getNewRoomType().getRoomtypeNum() + 1);
        roomDao.changgeRoomTypeNum(room.getNewRoomType());
        return roomDao.insertRoom(room);
    }

    /**
     * 修改房间管理
     *
     * @param room 房间管理
     * @return 结果
     */
    @Override
    public int updateoom(RoomDto room) {
        room.setUpdateTime(new Date());
        room.getOldRoomType().setRoomtypeNum(room.getOldRoomType().getRoomtypeNum() - 1);
        room.getNewRoomType().setRoomtypeNum(room.getNewRoomType().getRoomtypeNum() + 1);
        roomDao.changgeRoomTypeNum(room.getOldRoomType());
        roomDao.changgeRoomTypeNum(room.getNewRoomType());
        return roomDao.updateoom(room);
    }

    /**
     * 批量删除房间管理
     *
     * @param roomIds 需要删除的房间管理主键
     * @return 结果
     */
    @Override
    public int deleteRoomByRoomIds(Long[] roomIds) {
//        return roomDao.deleteRoomByRoomIds(roomIds);
        return 0;
    }

    /**
     * 删除房间管理信息
     *
     * @return 结果
     */
    @Override
    public int deleteRoomByRoomId(RoomDto room) {
        room.getOldRoomType().setRoomtypeNum(room.getOldRoomType().getRoomtypeNum() - 1);
        roomDao.changgeRoomTypeNum(room.getOldRoomType());
        return roomDao.deleteRoomByRoomId(room.getRoomId());
    }

    /**
     * 获得所有的房间类型
     *
     * @return
     */
    @Override
    public List<Roomtype> selectroomtypeList() {
        List<Roomtype> list = redisTemplate.opsForList().range(ROOMTYPEKEY, 0, -1);
        if (list.size() != 0) {
            return list;
        } else {
            list = roomDao.selectroomtypeList();
            if (list.size() != 0) {
                redisTemplate.opsForList().leftPushAll(ROOMTYPEKEY, list);
                return list;
            } else {
                return list;
            }
        }
    }
}
